﻿using System;

namespace ConsoleApp3_c5
{
    class Program
    {
        //定义一个闹钟类，包含两个事件：Tick和Alarm
        public class AlarmClock
        {
            public event EventHandler Tick;
            public event EventHandler Alarm;

            //启动闹钟，每秒触发一次Tick事件，当闹钟响起时触发Alarm事件
            public void Start(int ticksBeforeAlarm)
            {
                for (int i = 0; i < ticksBeforeAlarm; i++)
                {
                    Thread.Sleep(1000);
                    Tick?.Invoke(this, EventArgs.Empty);
                }
                Alarm?.Invoke(this, EventArgs.Empty);
            }
        }

        static void Main(string[] args)
        {
            //创建闹钟对象
            var clock = new AlarmClock();

            //订阅闹钟事件
            clock.Tick += (s, e) => Console.WriteLine("滴答滴答");
            clock.Alarm += (s, e) => Console.WriteLine("闹钟响铃了！");
            
            //启动闹钟，响铃时间为3秒
            clock.Start(3);
        }
    }
}